﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Mvc.Html;

namespace MyNeighbourhood.Helpers
{
    public static class HelperMethods
    {
        public static bool IsIpInRange(string startIpAddr, string endIpAddr, string address)
        {
            long ipStart = BitConverter.ToInt32(IPAddress.Parse(startIpAddr).GetAddressBytes().Reverse().ToArray(), 0);
            long ipEnd = BitConverter.ToInt32(IPAddress.Parse(endIpAddr).GetAddressBytes().Reverse().ToArray(), 0);
            long ip = BitConverter.ToInt32(IPAddress.Parse(address).GetAddressBytes().Reverse().ToArray(), 0);
            return ip >= ipStart && ip <= ipEnd;
        }

        public static MvcHtmlString ActionImageLink(this HtmlHelper helper, string imageUrl, string imageClass, string imageId, string onclick, string actionName, string controller, object routeValues)
        {
            var builder = new TagBuilder("img");
            builder.MergeAttribute("src", imageUrl);
            builder.MergeAttribute("class", imageClass);
            builder.MergeAttribute("id", imageId);
            builder.MergeAttribute("onclick", onclick);
            var link = helper.ActionLink("[replaceme]", actionName, controller, routeValues);
            return new MvcHtmlString(link.ToHtmlString().Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing)));
        }

        public static MvcHtmlString ActionImageLink(this AjaxHelper helper, string imageUrl, string title, string actionName, string controller, object routeValues, AjaxOptions ajaxOptions)
        {
            var builder = new TagBuilder("img");
            builder.MergeAttribute("src", imageUrl);
            builder.MergeAttribute("title", title);
            var link = helper.ActionLink("[replaceme]", actionName, controller, routeValues, ajaxOptions);
            return new MvcHtmlString(link.ToHtmlString().Replace("[replaceme]", builder.ToString(TagRenderMode.SelfClosing)));
        }
    }
}